//
//  CLProperty.m
//  RuntimeTest
//
//  Created by Charles on 16/9/26.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLProperty.h"
#import <objc/runtime.h>
#import <objc/message.h>
@implementation CLProperty

- (void)getAllProperties{
    unsigned int count = 0;
    
    objc_property_t * properties = class_copyPropertyList(self.class, &count);
    
    for (unsigned int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        const char * propertyName = property_getName(property);
        const char * propertyAttributes = property_getAttributes(property);
        NSLog(@"%s  %s", propertyName, propertyAttributes);
        unsigned int count = 0;
        objc_property_attribute_t * attributes = property_copyAttributeList(property, &count);
        for (unsigned int i=0; i<count; i++) {
            objc_property_attribute_t attribute = attributes[i];
            const char * name = attribute.name;
            const char * value = attribute.value;
            NSLog(@"name: %s   value: %s", name, value);
        }
        free(attributes);
    }
    free(properties);
}

- (void)getAllMemberVariables{
    unsigned int count = 0;
    Ivar * ivars = class_copyIvarList(self.class, &count);
    for (unsigned int i = 0; i<count; i++) {
        Ivar ivar = ivars[i];
        const char * name = ivar_getName(ivar);
        const char * type = ivar_getTypeEncoding(ivar);
        NSLog(@"name: %s encodeType: %s", name, type);
    }
    free(ivars);
}

+ (void)test{
    CLProperty * property = [[CLProperty alloc]init];
    
    void (*callMethod)(id,SEL) = (void (*)(id,SEL))objc_msgSend;
    callMethod((id)property,@selector(getAllProperties));
    
    ((void(*)(id,SEL))objc_msgSend)((id)property,@selector(getAllMemberVariables));
    
}

@end
